package com.git.smp.utils;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.git.smp.cache.DataDictionaryCache;
import com.git.smp.controller.SmpServiceController;
import com.git.smp.entity.MappingNameInfo;
import com.git.smp.entity.SmpField;

public class InitMappingFile {
	static Logger logger = LoggerFactory.getLogger(InitMappingFile.class);
	public static Document createMappingFile(List<SmpField> listField, MappingNameInfo mappingNameInfo) {
		
		logger.info("导出mappingFile"+mappingNameInfo.getSystemShort()+"_"+mappingNameInfo.getPubCode());
		Document doc = DocumentHelper.createDocument();
		Element Transaction = doc.addElement("Transaction");
		Transaction.addAttribute("system", mappingNameInfo.getSystemShort());
		Transaction.addAttribute("trancode",mappingNameInfo.getPubCode());
		
		Element Request = Transaction.addElement("Request");
		Element Response = Transaction.addElement("Response");
		Element RequestBody = Request.addElement("Body");
		Element ResponseBody = Response.addElement("Body");
		Element request = RequestBody.addElement("request");
		Element response = ResponseBody.addElement("response");	
		Element RequestBizHeader = request.addElement("bizHeader");
		Element ResponseBizHeader = response.addElement("bizHeader");
		Element RequestBizBody = request.addElement("bizBody");
		Element ResponseBizBody = response.addElement("bizBody");
		try {
			createElement(RequestBizHeader, listField, "01", "01","");
			createElement(RequestBizBody, listField, "02", "01","");
			createElement(ResponseBizHeader, listField, "01", "02","");
			createElement(ResponseBizBody, listField, "02", "02","");
		}catch(Exception e) {
			logger.error("导出mappingFile"+mappingNameInfo.getSystemShort()+"_"+mappingNameInfo.getPubCode()+"出错",e);
		}
		return doc;
	}
	private static void createElement(Element e1, List<SmpField> listField, String struct, String inOut,String parent) {
		
		for(SmpField smpField : listField) {
			if(smpField.getParentType()==null) {
				smpField.setParentType("");
			}
			
			if(struct.equals(smpField.getStruct())&& inOut.equals(smpField.getInOut()) && parent.equals(smpField.getParentType())) {
				if("01".equals(smpField.getNodeType())) {
					Element element = e1.addElement(smpField.getFieldCode());
					element.addAttribute("type",DataDictionaryCache.getItemName("SMP0018", smpField.getFieldType()));
					element.addAttribute("length", smpField.getFieldLength());
					element.addAttribute("defaultValue", "");
					element.addAttribute("describe", smpField.getFieldName());
				}else if("03".equals(smpField.getNodeType())) {
					Element element = e1.addElement(smpField.getFieldCode());
					createElement(element, listField, struct, inOut,smpField.getFieldCode());
				}else if("02".equals(smpField.getNodeType())) {
					Element element = e1.addElement(smpField.getFieldCode());
					element.addAttribute("type", "group");
					createElement(element, listField, struct, inOut,smpField.getFieldCode());
				}
			}
		}
	} 
	
}
